package com.wp.utils;

import java.util.Arrays;
import java.util.Random;

/**
 * Created by 王萍 on 2017/9/18 0018.
 */
public class LinkedListUtil {

    public static class Node {
        public int value;
        public Node next;

        public Node(int value) {
            this.value = value;
        }
    }

    //打印链表节点值
    public static void printNode(Node node) {
        StringBuilder builder = new StringBuilder("");
        while (node != null) {
            builder.append(node.value).append("->");
            node = node.next;
        }
        System.out.println(builder.substring(0, builder.length() - 2));
    }

    public static void printNodeExceptFirst(Node node) {
        StringBuilder builder = new StringBuilder("");
        node = node.next;
        while (node != null) {
            builder.append(node.value).append("->");
            node = node.next;
        }
        System.out.println(builder.substring(0, builder.length() - 2));
    }

    public static Node produceList(int length) {
        Random random = new Random();
        Node head = new Node(random.nextInt(10));
        Node node;
        for (int i = 0; i < length - 1; i++) {
            node = new Node(random.nextInt(10));
            node.next = head.next;
            head.next = node;
        }
        return head;
    }

    public static Node produceSortedList(int length) {
        Random random = new Random();
        int[] array = new int[length];
        for (int i = 0; i < length; i++) {
            array[i] = random.nextInt(10);
        }
        Arrays.sort(array);
        Node head = new Node(array[0]);
        Node current = head;
        Node node;
        for (int i = 1; i < length; i++) {
            node = new Node(array[i]);
            current.next = node;
            current = current.next;
        }
        return head;
    }
}
